package com.example.demo.thread;

import com.example.demo.service.DZDPScratchService;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ZhengWei
 * @Description
 * @date 2020/8/6 2:31 下午
 */
public class RerunSkuTask implements Runnable {
    private DZDPScratchService scratchService;

    private int cityId;

    private AtomicInteger index;

    private ArrayList<Integer> skuIdList;

    public RerunSkuTask(DZDPScratchService scratchService, int cityId, ArrayList<Integer> skuIdList, AtomicInteger index) {
        this.cityId = cityId;
        this.scratchService = scratchService;
        this.index = index;
        this.skuIdList = skuIdList;
    }

    @Override
    public void run() {
        int tempIndex = index.getAndDecrement();
        while (tempIndex > -1) {
            Integer skuId = skuIdList.get(tempIndex);
            scratchService.scratchSku(cityId, skuId);
            System.out.println(Thread.currentThread() + " 剩余任务数：" + index.get());
            tempIndex = index.getAndDecrement();
        }
    }
}
